package com.shwread.android.ui.page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.ciko.code.http.BaseRequest;
import com.ciko.code.http.BaseResponse;
import com.ciko.code.http.HttpClient;
import com.ciko.code.http.HttpResponseListener;
import com.mywork.WorkUIKit;
import com.netease.nim.demo.base.YXBaseActivity;
import com.shwread.android.qysw.activity.BaseSimpleActivity;
import com.shwread.android.qysw.activity.NewLoginActivity;
import com.shwread.android.qysw10000243.R;
import com.shwread.android.ui.page.chat.SessionPage;
import com.tgx.tina.android.ipc.framework.BaseActivity;
import com.tgx.tina.android.ipc.framework.IUIPage;

import logic.beanenum.BookStatus_Enum;
import logic.beanenum.BookType_Enum;
import logic.constants.Const;
import logic.constants.DefaultConsts;
import logic.dao.UserDao;
import logic.util.AppUtils;


public abstract class AbstractUIPage<T extends BaseActivity>
        implements
        IUIPage<Activity>,
        HttpResponseListener {
    private UserDao userDao;

    public AbstractUIPage(T t) {
        setActivity(t);
    }

    public AbstractUIPage<T> setActivity(T t) {
        context = bActivity = t;
        return this;
    }

    protected Context context;
    protected View curMyView;
    protected T bActivity;

    @Override
    public void dispose() {
        curMyView = null;
    }

    @Override
    public boolean isDisposable() {
        return true;
    }

    @Override
    public View getView() {
        return curMyView;
    }

    @Override
    public boolean isHistoryInclude() {
        return true;
    }

    @Override
    public boolean handleBack() {
        return false;
    }

    public void onMenuClick() {

    }

    protected void setBookTypeIcon(ImageView img, String type) {
        if (TextUtils.isEmpty(type)) return;
        if (type.equals(BookType_Enum.LISTEN)) {
            img.setVisibility(View.VISIBLE);
            img.setBackgroundResource(R.drawable.listen_icon);
        } else {
            img.setVisibility(View.GONE);
        }
    }

    protected void setBookShlefTypeIcon(ImageView img, int status, String type) {
        if (TextUtils.isEmpty(type)) return;
        img.setVisibility(View.VISIBLE);
        //TODO 待完善
        if (type.equals(BookType_Enum.LISTEN.getValue())) {
            img.setBackgroundResource(R.drawable.listen_icon);
        } else if (status == BookStatus_Enum.ONLINE.getValue()) {
            img.setBackgroundResource(R.drawable.cloud_icon);
        } else if (status == BookStatus_Enum.DOWNLOAD.getValue()) {
            img.setBackgroundResource(R.drawable.download_icon);
        } else if (status == BookStatus_Enum.REFERRAL.getValue()) {
            img.setBackgroundResource(R.drawable.cloud_icon);
        } else {
            img.setVisibility(View.GONE);
        }
    }

    protected void intent2Acticity(Class<?> cls) {
        Intent intent = new Intent(bActivity, cls);
        context.startActivity(intent);
    }

    protected void intent2Acticity(Class<?> cls, String title) {
        Intent intent = new Intent(bActivity, cls);
        intent.putExtra("title", title);
        context.startActivity(intent);
    }

    public Context getContext() {
        return context;
    }

    @Override
    public void onResponseSuccess(BaseResponse<?> response, String tag) {

        BaseResponse obj = (BaseResponse) response;
        Log.e("Success", obj.getResultCode() + "");
        if (obj.getResultCode() == 1003) {
            String StrObject = (String) obj.getReturnObject();
            Log.e("StrObject", StrObject + "");
            Intent intent = new Intent();
            intent.setAction(DefaultConsts.EXCEPTION_LOGIN_STRING);
            intent.putExtra("json", "1003");
            intent.putExtra("packageName", AppUtils.getPackageName(context));
            context.sendBroadcast(intent);
        }
    }

    @Override
    public void onRequestStart(String tag) {

    }

    @Override
    public void onResponseFail(String tag, int resultCode) {
        if (resultCode == 1100) {
//			userDao = UserDao.getInstance(context);
//			UserBean tp = UserDao.getInstance(context).getLastLoginUser();
//			if(tp!=null){
//				Log.e("删除user", tp.toString());
//				userDao.deleteUser(tp.getAccount());
//			}else{
//				Log.e("user","空的");
//			}
            WorkUIKit.setUser(null);
            Const.clear();
//			context.startActivity(new Intent(context, NewLoginActivity.class));
            clickChangeAccount();
        } else if (resultCode == 4) {
            Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show();
//			userDao = UserDao.getInstance(context);
//			UserBean tp = UserDao.getInstance(context).getLastLoginUser();
//			if(tp!=null){
//				Log.e("删除user", tp.toString());
//				userDao.deleteUser(tp.getAccount());
//			}else{
//				Log.e("user","空的");
//			}
            WorkUIKit.setUser(null);
            Const.clear();
//			context.startActivity(new Intent(context, NewLoginActivity.class));
            clickChangeAccount();
        }
    }

    void clickChangeAccount() {
        SessionPage.isFirstIn = 2;
        BaseSimpleActivity.clearNotification(bActivity, -1);
        BaseSimpleActivity.exit();
        YXBaseActivity.exit();
        intent2Acticity(NewLoginActivity.class);
        bActivity.finish();
    }

    protected void doRequestHttp(BaseRequest request) {
        HttpClient.getInstance(context).post(request, this);
    }
}
